package com.fone.player.online.adapter;

import java.util.List;

import com.fone.player.R;
import com.fone.player.entity.VipInfo;
import com.fone.player.util.FoneUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class VipAdapter extends BaseAdapter {
	
	/** 日志标签 */
//	private static final String TAG = "VipAdapter";

//	private ArrayList<Cnt> vipList;
	private List<VipInfo> vipList;

	private LayoutInflater layoutInflater;
	
	DisplayImageOptions options;

	private String shost;

	public VipAdapter(Context context, String shost, List<VipInfo> vipList) {
		super();
		
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.default_342_456)
		.showImageForEmptyUri(R.drawable.default_342_456)
		.showImageOnFail(R.drawable.default_342_456)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.build();
		
		initData(shost, vipList);
		layoutInflater = LayoutInflater.from(context);
	}

	public void initData(String shost, List<VipInfo> vipList) {
		this.shost = shost;
		this.vipList = vipList;
		
	}

	@Override
	public int getCount() {
		return vipList.size();
	}

	@Override
	public Object getItem(int position) {
		return vipList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		VipInfo info = vipList.get(position);

		ViewHolder vHolder = null;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.item_vip, null);
			// 创建 ViewHodler 对象
			vHolder = new ViewHolder();
			vHolder.pic = (ImageView) convertView.findViewById(R.vip.pic);
			vHolder.title = (TextView) convertView.findViewById(R.vip.name);
			vHolder.score = (TextView) convertView.findViewById(R.vip.score);
			// 设置 Tag
			convertView.setTag(vHolder);
		} else {
			vHolder = (ViewHolder) convertView.getTag();
		}

		//ImageDownloadModule.getInstance().download(FoneUtil.getAbsoluteUrl("", shost, info.pic1), R.drawable.default_184,vHolder.pic);
		
		ImageLoader.getInstance().displayImage(FoneUtil.getAbsoluteUrl("", shost, info.pic1), vHolder.pic, options);
		
		vHolder.title.setText(info.name);
		vHolder.score.setText(setQuality(info.quality));
		return convertView;
	}

	static class ViewHolder {
		ImageView pic;
		TextView title;
		TextView score;
	}
	
	
	/**
	 * 推荐维度 0 无纬度 1 时长 2 评分 3 更新至 4 自定义文字 5 自定义图片
	 */
	private String setQuality(String quality){
		if(TextUtils.isEmpty(quality)){
			return "暂无分数";
		}else{
			return FoneUtil.String2Integer(quality)*2+"分";
		}
	}
	
}
